package org.example.http;

import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.HttpRequest;
import lombok.Data;

import java.util.HashMap;
import java.util.Map;

@Data
public class NRequest {
    private String url;
    private String method;
    private Map<String, Object> parametersMap = new HashMap<>();

    private ChannelHandlerContext ctx;
    private HttpRequest req;

    public NRequest(ChannelHandlerContext ctx, HttpRequest req) {
        this.ctx = ctx;
        this.req = req;
        init();
    }

    private void init() {
        this.method = req.method().toString();
        String[] requestUrl = req.uri().split("\\?");
        this.setUrl(requestUrl[0]);

        if (requestUrl.length > 1) {
            String[] paramKeyValue = requestUrl[1].split("&");
            for (String keyValue : paramKeyValue) {
                String[] params = keyValue.split("=");
                this.getParametersMap().put(params[0], params[1]);
            }
        }
    }
}
